package ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import ware.domain.PurchaseDetail;
import ware.domain.WareInfo;
import ware.service.PurchaseDetailService;
import ware.mapper.PurchaseDetailMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author panghu
 * @description 针对表【wms_purchase_detail】的数据库操作Service实现
 * @createDate 2023-03-04 21:05:07
 */
@Service
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailMapper, PurchaseDetail>
        implements PurchaseDetailService {

    @Override
    public Page<PurchaseDetail> querPage(Map<String, Object> params) {
        LambdaQueryWrapper<PurchaseDetail> queryWrapper = getQueryWrapper(params);
        Page<PurchaseDetail> wareInfoPage = new Page<>(Integer.parseInt((String) params.get("page")), Integer.parseInt((String) params.get("limit")));
        return this.baseMapper.selectPage(wareInfoPage, queryWrapper);

    }

    @Override
    public List<PurchaseDetail> listDetailByPurchaseId(Long id) {
        // 按照id找采购项
        return this.list(new LambdaQueryWrapper<PurchaseDetail>().eq(PurchaseDetail::getPurchaseId, id));
    }

    private LambdaQueryWrapper<PurchaseDetail> getQueryWrapper(Map<String, Object> params) {
        LambdaQueryWrapper<PurchaseDetail> wrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(params.get("key"))) {
            Object key = params.get("key");
            wrapper.or().eq(PurchaseDetail::getPurchaseId, key)
                    .or().eq(PurchaseDetail::getSkuId, Long.parseLong(key.toString()))
                    .or().eq(PurchaseDetail::getSkuPrice, key)
                    .or().eq(PurchaseDetail::getSkuNum, key)
            ;
        }
        wrapper.eq(!ObjectUtils.isEmpty(params.get("status")), PurchaseDetail::getStatus, params.get("status"));
        wrapper.eq(!ObjectUtils.isEmpty(params.get("wareId")), PurchaseDetail::getWareId, params.get("wareId"));
        return wrapper;
    }


}




